package com.atguigu.gulimall.ware.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @DESCRIPTION: MQ 的相关配置
 * @author: zr
 * @DATE: 2023/5/21 16:06
 */
@Configuration
public class MyRabbitConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 配置消息转换器
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
     *
     * @return
     */
    @Bean
    public Exchange orderEventChange() {
        return new TopicExchange("stock-event- exchange", true, false);
    }
    /**
     * x-dead-letter-exchange: order-event-exchange
     * x-dead-letter-routing-key: order.release.order
     * x-message-ttl: 60000
     * 这个地方参数要设置正确才可以
     */
    @Bean
    public Queue stockDelayQueue() {
        // 持久化 排它
        // String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "stock-event-exchange");
        arguments.put("x-dead-letter-routing-key", " stock.release.stock");
        arguments.put("x-message-ttl", 120000);
        Queue queue = new Queue("order.delay.queue", true, false, false,arguments );
        return queue;
    }

    @Bean
    public Queue stockReleaseStockQueue() {
        Queue queue = new Queue("stock.release.stock.queue", true, false, false);
        return queue;
    }


    /**
     * String destination, DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments
     *
     * @return
     */
    @Bean
    public Binding stockLockedBinding() {
        return    new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",null
        );
    }

    @Bean
    public Binding stockReleaseBinding() {
        return    new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",null
        );
    }
}
